-- tests scene
local layer = nil

--------------------- 类
ShopItemClass = {ach=0}
--这句是重定义元表的索引，必须要有，
ShopItemClass.__index = ShopItemClass
--模拟构造体，一般名称为new()
function ShopItemClass:new(_ach, _batchNode)
    local self = {}   
    setmetatable(self, ShopItemClass)   --必须要有
    self.ach = _ach
    self.achInfo = ShopItems_Infos[_ach]
    
    self._batchNode = _batchNode
    
    --背景
    self.spr_bg = CCSprite:createWithSpriteFrameName("ShopItems.png")
    self._batchNode:addChild(self.spr_bg)
    --名称
    self.lb_tpe = CCLabelTTF:create(self.achInfo[PName], "Arial", FontSize)
    self.lb_tpe:setAnchorPoint(ccp(0, 0.5))--对齐方式
    self.lb_tpe:setColor(ccc3(146, 162, 225))--颜色
    
    --价格  table.concat({self.money, self._unit}
    self.money = self.achInfo[PCount]
    self._unit = "水晶币"
    vcolor = ccc3(170, 96, 59)
    if self.money == nil then
        if self.achInfo[PScore] ~= nil then
            self.money = self.achInfo[PScore]
            self._unit = "元"
            vcolor = ccc3(255, 255, 25)
        elseif self.achInfo[PLevel] ~= nil then
            self.money = self.achInfo[PLevel]
            self._unit = "魂力"
            vcolor = ccc3(114, 215, 255)
        end
    end
    self.lb_sco = CCMenuItemLabel:create(CCLabelTTF:create(table.concat({self.money,self._unit}), "Arial", FontSize))
    self.lb_sco:setColor(vcolor)--颜色
    self.lb_sco:setAnchorPoint(ccp(1, 0.5))
    
    
    --self.lb_sco = GameNumbers:new(vNumberCrytal, NumberAlginment_Right)
    --self.lb_sco:setNumber(self.money)
    
    
    --数量
    self.lb_count = CCMenuItemLabel:create(CCLabelTTF:create("1111111", "Arial", FontSizeMin))
    --self.lb_count:setColor(vcolor)--颜色
    self.lb_count:setAnchorPoint(ccp(1, 0.5))
    self:UpdateCount()
    
    --数量背景
    self.priceBg = CCSprite:createWithSpriteFrameName("levelBg.png")
    self._batchNode:addChild(self.priceBg,0, self.ach)
    
    
    if self.ach == Item_Crystal99 or self.ach == Item_Crystal250 or self.ach == Item_HuiliCrystal then
        self.lb_count:setVisible(false)
        self.priceBg:setVisible(false)
    end
    
    --图标
    local _spw = 86
    local _sph = 86
    self.itemIcon = CCSprite:createWithSpriteFrameName(self.achInfo[PModel])
    self._batchNode:addChild(self.itemIcon)
    
    --购买
    self._btnBuy = nil
    
    return self  
end
function ShopItemClass:getPosition()

end
function ShopItemClass:setPosition(x,y)
    self.spr_bg:setPosition(gdccp(x,y))
    self.lb_tpe:setPosition(gdccp(x-20,y+45))
    self.lb_sco:setPosition(gdccp(x-20,y-45))
    
    self.itemIcon:setPosition(gdccp(x-80,y + 20))
    
    if self._btnBuy ~= nil then
        self._btnBuy:setPosition(gdccp(x+80,y-40))
    end
    
    self.lb_count:setPosition(gdccp(x + 100,y))
    self.priceBg:setPosition(gdccp(x + 85,y))
end
function ShopItemClass:addToChild(layer)
    layer:addChild(self.lb_tpe)
    
    layer:addChild(self.lb_sco)
    --self.lb_sco:addLayer(layer)
    
    layer:addChild(self.lb_count,1)
end
-- 更新数量显示
function ShopItemClass:UpdateCount()
    --print("item__",GetItems(self.ach))
    self.lb_count:setString(GetItems(self.ach))
    
    --更新按钮状态(禁用不可购买道具
    if self.achInfo[PEff] ~= nil and GetItems(self.ach) ~= nil and GetItems(self.ach) >= self.achInfo[PEff] then
        if self._btnBuy then
            self._btnBuy:setEnabled(false)
        end
    end
        
    ---体力
    if self.ach == Item_Tili then
        self.lb_count:setString(GetTili())
    end
end

local _lblSoul = nil
local _txtMyCrystal = nil
GameFunc_UpdateCry = function()
    ---更新水晶显示
    if _txtMyCrystal then
        _txtMyCrystal:setNumber(GetCrystal())
    end
    if _lblSoul then
        _lblSoul:setNumber(GetSoulPower())
    end
end


local function initWithLayer(stage)
    
    local function enterLayer()
    end
    
    local function exitlayer()
        _lblSoul = nil
        _txtMyCrystal = nil
    end
    
    layer = GFunc_CreateLayerEnterOrExit(nil, nil, enterLayer, exitlayer)
    
    -- 水晶
    local _gui_hl = CCSprite:createWithSpriteFrameName("GUI_SJ.png")
    _gui_hl:setPosition(gdccp(50, DWinSizeHeight - 70))
    layer:addChild(_gui_hl, LayerIdx_Gui)
    
    _txtMyCrystal = GameNumbers:new(vNumberCrytal, NumberAlginment_Left)
    _txtMyCrystal:addLayer(layer, LayerIdx_Gui)
    _txtMyCrystal:setNumber(GetCrystal())
    _txtMyCrystal:setPosition(gdccp(70, DWinSizeHeight - 75))
    
    
    -- 魂力
    local _gui_hl = CCSprite:createWithSpriteFrameName("Huili.png")
    _gui_hl:setPosition(gdccp(440, DWinSizeHeight - 70))
    _gui_hl:setAnchorPoint(ccp(1, 0.5))
    layer:addChild(_gui_hl, LayerIdx_Gui)
    
    _lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Left)
    _lblSoul:addLayer(layer, LayerIdx_Gui)
    _lblSoul:setNumber(GetSoulPower())
    _lblSoul:setPosition(gdccp(450, DWinSizeHeight - 75))
    
    
    local tableItems = {}
    
    
    -- 按钮回调函数
    local function menuCallback(itemTag)
        -- 人民币购买
        if(ShopItems_Infos[itemTag][PScore] ~= nil and ShopItems_Infos[itemTag][PScore] > 0) then
            Play_Effect(sme_Click)
            
            if itemTag == Item_Crystal99 then
                toJavaBuyItems(GAYX_Item_CY99)
            elseif itemTag == Item_Crystal250 then
                toJavaBuyItems(GAYX_Item_CY230)
            end
            return
        -- 魂值购买
        elseif ShopItems_Infos[itemTag][PLevel] ~= nil then
            if ShopItems_Infos[itemTag][PLevel] <= GetSoulPower() then
                Play_Effect(sme_BuyEnd)
                
                --减少魂力
                AddSoulPower(-ShopItems_Infos[itemTag][PLevel])
                --增加水晶
                AddCrystal(1)
            else
                Play_Effect(sme_ClickError)
                return
            end
        -- 是否有足够的水晶币
        elseif ShopItems_Infos[itemTag][PCount] ~= nil then
            if ShopItems_Infos[itemTag][PCount] <= GetCrystal() then
        
                --扣除水晶币
                AddCrystal(-ShopItems_Infos[itemTag][PCount])
                
                --如果是体力道具,则直接增加体力
                if itemTag == Item_Tili then
                    AddTili(1)
                else
                    UpItems(itemTag, 1)
                end
                
                tableItems[itemTag]:UpdateCount()
            else
                Play_Effect(sme_ClickError)
                return
            end
        end
        
        Play_Effect(sme_BuyEnd)
        --更新水晶币显示
        _txtMyCrystal:setNumber(GetCrystal())
        --魂力值更新
        _lblSoul:setNumber(GetSoulPower())
    end
    
    -------- ui
    
    
    -- 背景
    local _bgSpr = nil
    
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(_bgSpr)
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bgHead)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 960-32))
    layer:addChild(_bgSpr, 9)
    
    _bgSpr = CCSprite:createWithSpriteFrameName(s_ShopBg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(_bgSpr)
    
    --- 加载生成成就列表
    -- 已经完成的成就优先排序
    local _achlist = {}
    local _bpx = 170
    local _bpy = 730
    local _invH = 160
    local _invW = 300
    local i = 0
    local sum = 0
        
    local _layer_items = nil
    local mainMenu = nil
    local _batchNode = nil
    
    ---翻页容器
    local container = GameContainerPage:new(DWinSizeWidth, 0)
    container:addToLayer(layer)
    
    local ipage = -1
    
    while i <= table.maxn(ShopItems_Infos) do
        if ShopItems_Infos[i] ~= nil then
            local pageindex = math.floor(sum / 8)
            if pageindex ~= ipage then
                ipage = pageindex
                
                mainMenu = CCMenu:create()
                _layer_items = CCLayer:create()
                mainMenu:setPosition(gdccp(0,0))
                -- 主菜单大小
                mainMenu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
                _layer_items:addChild(mainMenu,1)
                
                _batchNode = CCSpriteBatchNode:create(s_MainUI2)
                _layer_items:addChild(_batchNode)
                
                container:addChild(_layer_items)
            end
            
            local cach = ShopItemClass:new(i, _batchNode)
            --购买按钮
            local btnItem = GFunc_CreateButtonDP("item_buy.png", menuCallback)
            mainMenu:addChild(btnItem,0,i)
            cach._btnBuy = btnItem
            tableItems[i] = cach
            --是否达到了购买上限
            if(cach.achInfo[PEff] ~= nil and GetItems(i) ~= nil and GetItems(i) >= cach.achInfo[PEff]) then
                btnItem:setEnabled(false)
            end
            
            cach:setPosition(_bpx + sum%2*_invW, _bpy - math.floor((sum%8)/2) * _invH)
            cach:addToChild(_layer_items)
            sum = sum + 1
        end
        i = i + 1
    end

    return layer
end

function Create_Shop_Scene(stage)
    local scene = CCScene:create()
    --layer = CCLayer:create()

    initWithLayer(stage)

    scene:addChild(layer)
    scene:addChild(CreateBackMenuItem())

    return scene, UI_Shop
end



